<?php

namespace App\Http\Controllers\Partner;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Services\Sms;
use App\Models\Partner;
use App\Models\PartnerPrice;
use App\Services\RedisService;
use App\Services\AliSms;

class IndexController extends Controller
{
    

    public function reg()
    {
    	return view('partner.reg');
    }

    public function regDo(Request $request)
    {
        $rs['status'] = 'danger';
        $mobile = $request->mobile;
        $code = RedisService::get('wedding:sms:'.$mobile);
        if($code!=$request->validatecode){
            $rs['msg'] = '短信验证码错误';            
            return back()->withInput()->with('rs',$rs);
        }

        $data = $request->except(['_token','validatecode']);
        // 先查一下有没有注册过
        $partner = Partner::where(['mobile'=>$data['mobile']])->first();
        if($partner) {
            $request->session()->put('partner', $partner); 
            if(empty($partner->shop_pic)) {
              $rs['msg'] = '提示账号已注册，请登录';
              return redirect('partner/login')->with('rs',$rs);  
            }

            if($partner->status==0) {
                $rs['msg'] = '资料已经提交请耐心等待审核';
                return back()->withInput()->with('rs',$rs);
            } else {
                $rs['msg'] = '手机号码已注册请登录';
                return redirect('partner/login')->with('rs',$rs);
            }
        }

        $data['status'] = 0;
        $data['code']   = '2'.date('ymd').mt_rand(100,999);
        $partner = Partner::create($data);
        $request->session()->put('partner', $partner);
        return redirect('partner/reg-info');
    }

    /**
     * 提交资料
     */
    public function regInfo(Request $request)
    {       
       
        $data = session('partner');
        return view('partner.reg-info',compact('data'));
    }

    /**
     * 保存资料
     */
    public function infoSave(Request $request)
    {   
        $rs['status'] = 'danger';
        if(empty($request->shop_pic)) {
            $rs['msg'] = '请上传门店照片';
            return back()->withInput()->with('rs',$rs);
        }
        $data = $request->except(['_token','file']);
        $id = $request->id;
        Partner::where(['id'=>$id])->update($data);
        return redirect('partner/pending');
    
    }



    public function pending(Request $request)
    {        
        return view('partner.pending');
    }



    public function login()
    {
    	return view('partner.login');
    }

    public function loginDo(Request $request)
    {
        $rs['status'] = 'danger';
        $param   = $request->except(['_token']);

        $partner = Partner::where(['mobile'=>$param['mobile']])->first();
        
        if(!$partner) {
            $rs['msg'] = '账号错误';
            return back()->with('rs',$rs);
        } 

        if($partner->password != trim($param['password'])) { 


             $rs['msg'] = '密码错误';
            return back()->with('rs',$rs);
        }

        $request->session()->put('partner', $partner);

        if(empty($partner->shop_pic)){

            $rs['msg'] = '资料未提交，请提交资料';
            return redirect('partner/reg-info');   
        }

        if($partner->status == 0 ) {
            $rs['msg'] = '请耐心等待审核';
            return back()->with('rs',$rs);
        }
         
       // dd($rs); 
        return redirect('partner/price');
    }

    public function loginByMobile(Request $request)
    {
        $rs['status'] = 'danger';
        $param   = $request->except(['_token']);

        $partner = Partner::where(['mobile'=>$param['mobile']])->first();
        
        if(!$partner) {
            $rs['msg'] = '账号错误';
            return back()->with('rs',$rs);
        } 
        $mobile = trim($request->mobile);

        $code = RedisService::get('wedding:sms:'.$mobile);
        //dd($code,$param['validatecode']);
        if($code != trim($param['validatecode'])) { 


             $rs['msg'] = '验证码错误';
            return back()->with('rs',$rs);
        }

        if(empty($partner->shop_pic)){

            $rs['msg'] = '资料未提交，请提交资料';
            return redirect('partner/reg-info');   
        }

        if($partner->status == 0 ) {
            $rs['msg'] = '请耐心等待审核';
            return back()->with('rs',$rs);
        }
        $request->session()->put('partner', $partner); 
         
        $partner->login_times += 1;
        $partner->save();

        return redirect('partner/price');
    }

    public function logout(Request $request)
    {
        $request->session()->forget('partner');
        return redirect('partner/index');
    }

    public function forget()
    {
        return view('partner.forget');
    }

    public function forgetDo(Request $request)
    {   
        $rs['status'] = 'danger';
        $mobile = trim($request->mobile);
        $partner = Partner::where(['mobile'=>$mobile])->first();
        
        if(!$partner) {
            $rs['msg'] = '账号错误';
            return back()->with('rs',$rs);
        } 
        $code = RedisService::get('wedding:sms:'.$mobile);
        if($code != trim($request->validatecode)) { 

            $rs['msg'] = '验证码错误';
            return back()->with('rs',$rs);
        }

        $partner->password = trim($request->password);
        $partner->save();
        $rs['status'] = 'success';
        $rs['msg']    = '密码重置成功';
        return redirect('partner/login')->with('rs',$rs);
    }

    /**
     * 提交申请页面
     */
    public function submit()
    {
        return view('partner.submit');
    }

   

    public function index()
    {
        return view('partner.index');
    }

    public function account()
    {
        return view('partner.account');
    }

    public function accountDo(Request $request)
    {
        $rs['status'] = 'danger';
        $mobile = trim($request->mobile);

        if($mobile!=session('partner')->mobile) {
            $rs['msg'] = '手机号码与当前登录账号不匹配';
            return back()->with('rs',$rs);
        }

        // 检查下手机号是否已存在
        $p = Partner::where(['mobile'=>$mobile])->first();
        if($p) {
            $rs['msg'] = '手机号已被注册，请更换手机号';
            return back()->with('rs',$rs);
        }
        $code   = RedisService::get('wedding:sms:'.$mobile);
        if(empty($code) && $code != trim($request->validatecode)) { 

            $rs['msg'] = '验证码错误';
            return back()->with('rs',$rs);
        }
        $id   = session('partner')->id;
        $data = $request->except(['_token','validatecode']);
        $partner = Partner::where(['id'=>$id])->update($data);       
        
        

       
        $rs['status'] = 'success';
        $rs['msg']    = '账号修改成功';
        return redirect('partner/price')->with('rs',$rs);
    }

    public function modify()
    {
        $id = session('partner')->id;
        $data = Partner::find($id);
        return view('partner.modify',compact('data'));
    }

    public function modifyUpdate(Request $request)
    {
        $rs['status'] = 'danger';
        if(empty($request->shop_pic)) {
            $rs['msg'] = '请上传门店照片';
            return back()->withInput()->with('rs',$rs);
        }
        $data   = $request->except(['_token','file','validatecode']);
        $mobile = trim($data['mobile']);
        $code   = RedisService::get('wedding:sms:'.$mobile);
        if(empty($request->validatecode) || $code != trim($request->validatecode)) { 

            $rs['msg'] = '验证码错误';
            return back()->with('rs',$rs);
        }

        $id = $request->id;
        Partner::where(['id'=>$id])->update($data);
        $rs['status'] = 'success';
        $rs['msg'] = '资料修改成功';
        return redirect('partner/price')->with('rs',$rs);
    }

    public function price()
    {
        $prices_1 = PartnerPrice::where(['type_id'=>1])->get();
        $prices_2 = PartnerPrice::where(['type_id'=>2])->get();
        $prices_3 = PartnerPrice::where(['type_id'=>3])->get();
        return view('partner.price',compact('prices_1','prices_2','prices_3'));
    }


    public function sendSms(Request $request)
    {
        $rs['status'] = false;
        $mobile = $request->mobile;
        if(strlen($mobile)!=11) {
            $rs['msg'] = '手机号码错误';
            return response()->json($rs);
        }
        $flag = Sms::sendMsg($mobile);

    }

    public function upload(Request $request)
    {   
        $rs['status'] = true;
        if($request->file) {
            $rs['file'] = '/'.$request->file->store('uploads/partner');  
        }
        return response()->json($rs);
    }

}
